package com.ostrich.cloud.ostrichOrderService.app;

import cn.hutool.core.bean.BeanUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.ostrich.cloud.common.result.PageResult;
import com.ostrich.cloud.common.result.Result;
import com.ostrich.cloud.ostrichOrderService.client.OrderManagerClient;
import com.ostrich.cloud.ostrichOrderService.client.param.*;
import com.ostrich.cloud.ostrichOrderService.component.QRcodeUtils;
import com.ostrich.cloud.ostrichOrderService.component.WebSocketServer;
import com.ostrich.cloud.ostrichOrderService.infra.param.*;
import com.ostrich.cloud.ostrichOrderService.model.dto.*;
import com.ostrich.cloud.ostrichOrderService.service.service.Order1Service;
import com.ostrich.cloud.ostrichOrderService.service.service.ShoppingCarService;
import com.ostrich.cloud.ostrichOrderService.unit.GetCustomerId;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@Slf4j
public class OrderManagerApiImp implements OrderManagerClient {
    @Resource
    private Order1Service orderService;
    @Resource
    private ShoppingCarService shoppingCarService;
    @Resource
    private QRcodeUtils qRcodeUtils;
    @SneakyThrows
    @Override
    public Result createOrder(CreateParam param)  {
//        HttpServletRequest request1 = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//        String userId1 = request1.getHeader("customerId");
        Integer id = GetCustomerId.getParentId();
        CreateParamInfra createParamInfra = BeanUtil.copyProperties(param, CreateParamInfra.class);
        createParamInfra.setCustomerId(id);
        orderService.CreateOrder(createParamInfra);
        return Result.ok();
    }

    @Override
    public Result search(Long orderNo,Integer current,Integer size) {
        PageResult<ShoppingDto> shoppingDtoPageResult = orderService.searchOrder(orderNo, current, size);
        return Result.ok(shoppingDtoPageResult);
    }

    @Override
    public List<CustomerOrders> customerSearch() {
        return orderService.customerFindOrders(GetCustomerId.getParentId());
    }

    @Override

    public String changeOrderStatus(Long orderNo,Integer payType) {
       return orderService.pay(orderNo,qRcodeUtils.getQrCode(orderNo),payType);

    }

    @SneakyThrows
    @Override
    public Result changeOrderLocationId(Long orderNo, Integer locationId)  {
        orderService.changeOrderLocation(orderNo, locationId);
        return Result.ok();
    }

    @Override
    public Result createGroup(AddGroupParam param) {
        AddGroupParamInfra addGroupParamInfra = BeanUtil.copyProperties(param, AddGroupParamInfra.class);
        Integer id = GetCustomerId.getParentId();
        addGroupParamInfra.setCustomerId(id);
        orderService.group(addGroupParamInfra);
        return Result.ok();
    }

    @Override
    public OrderDto createKill(AddKillParam param) {
        AddKillParamInfra addKillParamInfra = BeanUtil.copyProperties(param, AddKillParamInfra.class);
        Integer id = GetCustomerId.getParentId();
        addKillParamInfra.setCustomerId(id);
        return orderService.secKill(addKillParamInfra);
    }

    @SneakyThrows
    @Override
    public Result delCar(int goodsId,int goodsNum) {
//        HttpServletRequest request1 = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//        String userId1 = request1.getHeader("customerId");
        Integer id = GetCustomerId.getParentId();

        shoppingCarService.delGoodsCar(id,goodsId,goodsNum);
        return Result.ok();
    }

    @SneakyThrows
    @Override
    public Result addCar(AddGoodsCarParam param)  {
        //        HttpServletRequest request1 = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//        String userId1 = request1.getHeader("customerId");
        Integer id = GetCustomerId.getParentId();

        AddGoodsCarParamInfra paramInfra = BeanUtil.copyProperties(param, AddGoodsCarParamInfra.class);
        log.info("car{}",param);
        paramInfra.setCustomerId(id);
        shoppingCarService.addGoodsCar(paramInfra);
        return Result.ok();
    }
    @SneakyThrows
    @Override
    public Result calOrder(Long orderNo) {
        orderService.userCancelOrder(orderNo);
        return Result.ok();
    }

    @Override
    public Result storeOrderSearch(@RequestParam("current")Integer current,
                                   @RequestParam("size")Integer size,
                                   @RequestParam("startTime")@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss") Date startTime,
                                   @RequestParam("finishTime")@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss") Date finishTime,
                                   @RequestParam("businessmanId") Integer businessmanId,@RequestParam("orderNo") Long orderNo) {
        StoreOrderSearchParamInfra storeOrderSearchParamInfra = new StoreOrderSearchParamInfra();

        storeOrderSearchParamInfra.setBusinessmanId(businessmanId);
        storeOrderSearchParamInfra.setCurrent(current);
        storeOrderSearchParamInfra.setFinishTime(finishTime);
        storeOrderSearchParamInfra.setOrderNo(orderNo);
        storeOrderSearchParamInfra.setSize(size);
        storeOrderSearchParamInfra.setStartTime(startTime);


        PageResult<StoreOrderDto> storeOrderDtoPageResult = orderService.StoreOrderFind(storeOrderSearchParamInfra);
        return Result.ok(storeOrderDtoPageResult);
    }


    @Override
    public Result searchGoods(Integer current,Integer size) {
        //        HttpServletRequest request1 = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//        String userId1 = request1.getHeader("customerId");
        Integer id = GetCustomerId.getParentId();
        PageResult<ShoppingCarDto> shoppingCarDtoPageResult = shoppingCarService.searchCar(id, current, size);
        return Result.ok(shoppingCarDtoPageResult);
    }

    @Override
    public Result changeDelivery(Integer riderId, Integer deliveryId, Long orderNo) {
        orderService.changeDeliveryInfo(riderId, deliveryId, orderNo);
        return Result.ok();
    }

    @Override
    public Result finishOrder(Long orderNo) {
        orderService.changeOrderStatusByStore(orderNo);
        return Result.ok();
    }

    @Override
    public Result cancelGroup(Long orderNo) {
        orderService.cancelGroup(orderNo);
        return Result.ok();
    }

    @Override
    public Result cancelAllGroup(Integer groupbySn) {
        orderService.cancelAllGroup(groupbySn);
        return Result.ok();
    }

    @Override
    public OrderAllGoods orderInfo(Long orderNo) {
        return orderService.searchUserOrder(orderNo);
    }

    @Override
    public Result cancelSec(Long orderNo) throws Exception {
        orderService.userCancelSecKill(orderNo);
        return Result.ok();
    }

    @Override
    public Result userFindOrder( Integer current, Integer size) {
        PageResult<UserOrderDto> userOrderDtoPageResult = orderService.userFindAllOrder(current, size);
        return Result.ok(userOrderDtoPageResult);
    }

    @Override
    public List<GroupCustomerDto> getGroupCustomer(Integer groupbySn) {
        return orderService.getGroupCustomer(groupbySn);
    }

    @Override
    public Result getUrl() {
        return null;
    }


}
